from django.shortcuts import render, get_object_or_404, HttpResponseRedirect
from .models import Post, Reply
from django.core.paginator import Paginator
from django.urls import reverse

# Create your views here.

def post(request, post_id):
    look_post = get_object_or_404(Post, id=post_id)
    all_page = look_post.replys.all()
    pag_page = Paginator(all_page, 10)
    page_1 = pag_page.get_page(1)
    start_page = 1
    end_page = pag_page.count / 10 + 1
    context = {'look_post': look_post, 'now_page': page_1, 'start_page': start_page, 'end_page': end_page}
    return render(request, 'posts/post.html', context=context)

def page(request, post_id, page_id):
    # if page_id == 1:
    #     return HttpResponseRedirect(reverse('posts:post', args=[post_id]))
    
    look_post = get_object_or_404(Post, id=post_id)
    all_page = look_post.replys.all()
    pag_page = Paginator(all_page, 10)
    now_page = pag_page.get_page(page_id)
    context = {'look_post': look_post, 'now_page': now_page, 'page_id': page_id}
    return render(request, 'posts/page.html', context=context)